﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using LearnNet.BLL;
using LearnNet.Common;
using LearnNet.DAL;
using LearnNet.Entity;
namespace BBSReport
{
    public partial class frmAddContent : Form
    {
        private bool isAdd = true;
        private List<SiteInfoEntity> SiteInfos = new List<SiteInfoEntity>();
        public frmAddContent()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ( string.IsNullOrEmpty(tbContent.Text))
            {
                MessageBox.Show("请填写完整");
                return;
            }

            ReportContentEntity rContent = new ReportContentEntity();
            rContent.SendContent = tbContent.Text;
            rContent.Type = comboBox1.SelectedValue.ToString();
            if (isAdd)
            {
                rContent.DataTable_Action_ = DataTable_Action.Insert;
                if (!string.IsNullOrEmpty(BLLHelper.ReportContentDisp(tbContent.Text).SendContent))
                {
                    MessageBox.Show("此信息已经存在");
                    return;
                }
                if (BLLHelper.ReportContentInsertUpdateDelete(rContent) > 0)
                {

                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                rContent.DataTable_Action_ = DataTable_Action.Update;
                rContent.Id = Utils.StringToInt(tbId.Text, 0);
                BLLHelper.ReportContentInsertUpdateDelete(rContent);
                MessageBox.Show("更新成功");
                isAdd = true;
                button1.Text = "新增";
            }
            tbContent.Text = "";
            tbId.Text = "";
            Bind();
        }
        protected void Bind()
        {
            List<ReportContentEntity> list = new List<ReportContentEntity>();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            int recordNum = 0;
            list = BLLHelper.ReportContentList(qp, out recordNum);
            // dataGridView1.DataSource = list;
            //dataGridView1.DataMember = "id,SiteUrl,SiteName";
            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = 4;

            dataGridView1.Columns[0].Name = "Id";
            dataGridView1.Columns[0].HeaderText = "id";
            dataGridView1.Columns[1].Name = "sendcontent";
            dataGridView1.Columns[2].Name = "type";

            foreach (ReportContentEntity rcontent in list)
            {
                int index = dataGridView1.Rows.Add();
                DataGridViewRow dr = dataGridView1.Rows[index];

                dr.Cells[0].Value = rcontent.Id;
                dr.Cells[1].Value = rcontent.SendContent;
                foreach (SiteInfoEntity siteInfo in SiteInfos)
                {
                    if (siteInfo.Id.ToString() == rcontent.Type)
                    {
                        dr.Cells[2].Value = siteInfo.SiteUrl;
                        break;
                    }
                }
               // dataGridView1.Rows.Add(dr);
            }
            

        }

        private void frmAddSite_Load(object sender, EventArgs e)
        {
            
            QueryParam qp=new QueryParam();
            qp.PageIndex=1;
            qp.PageSize=1000;
            int recordNum = 0;
            SiteInfos=BLLHelper.SiteInfoList(qp,out recordNum);
            SiteInfoEntity siteInfo = new SiteInfoEntity();
            siteInfo.Id = 0;
            siteInfo.SiteUrl = "共用内容";
            SiteInfos.Insert(0,siteInfo);
            comboBox1.DataSource = SiteInfos;
            comboBox1.DisplayMember = "SiteUrl";
            comboBox1.ValueMember = "Id";

            Bind();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = dataGridView1.CurrentCell.RowIndex; //获得行号
            if(string.IsNullOrEmpty( dataGridView1[1, rowIndex].Value.ToString())){
                return ;
            }
            if (MessageBox.Show("确定要删除" + dataGridView1[1, rowIndex].Value.ToString(), "删除确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
            {
                try
                {
                    ReportContentEntity rContent = new ReportContentEntity();
                    rContent.Id = Utils.StringToInt(dataGridView1[0, rowIndex].Value.ToString(), 0);
                    rContent.DataTable_Action_ = DataTable_Action.Delete;


                    BLLHelper.ReportContentInsertUpdateDelete(rContent);
                    Bind();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isAdd = true;
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isAdd = false;
            int rowIndex = dataGridView1.CurrentCell.RowIndex; //获得行号
            tbId.Text = dataGridView1[0, rowIndex].Value.ToString();
            tbContent.Text = dataGridView1[1, rowIndex].Value.ToString();
           
            for (int i = 0; i < comboBox1.Items.Count; i++)
            {

                if ((comboBox1.Items[i] as SiteInfoEntity).SiteUrl == dataGridView1[2, rowIndex].Value.ToString())
                {
                    comboBox1.SelectedIndex = i;
                    break;
                }
            }
             
            button1.Text = "保存";

        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bind();
        }
    }
}
